//
//  BaseTabBarViewController.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/1/29.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit

class BaseTabBarViewController: UITabBarController, CustomTabBarDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setUI()
        // Do any additional setup after loading the view.
    }
    
    func setUI() {
        UITabBar.appearance().backgroundImage = UIImage()
        let views = [HomeViewController(nibName: "\(HomeViewController.self)", bundle: nil), VideoViewController(nibName: "\(VideoViewController.self)", bundle: nil), MessageCircleViewController(nibName: "\(MessageCircleViewController.self)", bundle: nil), ShoppingViewController(nibName: "\(ShoppingViewController.self)", bundle: nil)]
        let imagesArr = ["essence", "new", "friendTrends", "ShoppingCart"]
        for (index, item) in views.enumerated() {
            var image:UIImage = UIImage(named: "tabBar_\(imagesArr[index])_icon")!
            var selectedimage:UIImage = UIImage(named: "tabBar_\(imagesArr[index])_click_icon")!;
            image = image.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
            selectedimage = selectedimage.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
            item.tabBarItem.image = image
            item.tabBarItem.selectedImage = selectedimage
            let navigationVC = MainNavigaionViewController(rootViewController: item)
            addChildViewController(navigationVC)
        }
        let bar = CustomTabBar()
        
        bar.customTabBarDelegate = self
        setValue(bar, forKey: "tabBar")
    }
    
    func tapNewPosts(index: NSInteger) {
        switch index {
        case 0:
            let richTextEditViewController = PublishViewController(nibName: "PublishViewController", bundle: nil)
                        let mainNavigation = MainNavigaionViewController(rootViewController: richTextEditViewController)
          
            self.selectedViewController?.present(mainNavigation, animated: true, completion: nil)

        case 1:
            let richTextEditViewController = PublishViewController(nibName: "PublishViewController", bundle: nil)
            richTextEditViewController.isVideo = true
            let mainNavigation = MainNavigaionViewController(rootViewController: richTextEditViewController)
            self.selectedViewController?.present(mainNavigation, animated: true, completion: nil)
            
        default:break
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
        // Dispose of any resources that can be recreated.
    }
    
}

protocol CustomTabBarDelegate: NSObjectProtocol {
    func tapNewPosts(index: NSInteger)
}

class CustomTabBar: UITabBar {
    
    lazy var publishButton: UIButton = {
        let button = UIButton(type: .custom)
        let sendImage = UIImage(named: "Home_release")
        button.setBackgroundImage(sendImage, for: .normal)
        button.frame.origin = CGPoint(x: 0, y: 5)
        button.sizeToFit()
        button.adjustsImageWhenHighlighted = false
        button.addTarget(self, action: #selector(clickPublishButton(_ :)), for: .touchUpInside)
        return button
        
    }()
    
    weak var customTabBarDelegate: CustomTabBarDelegate?
    
    @objc func clickPublishButton(_ btn: UIButton) {
        
        PublishSelectorView(["发文章", "发视频"], ["send_article","send_video"]).show({[unowned self]index in
            let vc = UIApplication.shared.delegate?.window??.rootViewController as? BaseTabBarViewController
            if(ONCommonManager.checkHasLoginIn(topVc: vc?.selectedViewController as? UINavigationController)){
                self.customTabBarDelegate?.tapNewPosts(index: index)
            }
        
        })
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        let image = UIImage(named: "bar_red_line")
        let imageView = UIImageView(image: image)
        imageView.frame = CGRect(x: 0, y: 0, width: ScreenWidth, height: 1)
        addSubview(imageView)
        addSubview(publishButton)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        self.backgroundColor = ONColor.tabBarBGColor
        let btnW = ScreenWidth / 5
        let btnY: CGFloat = 5
        var index: CGFloat = 0
        publishButton.center = CGPoint(x: self.bounds.size.width / 2, y: publishButton.center.y)
        for view in subviews {
            if view.isKind(of: NSClassFromString("UITabBarButton")!){
                let buttonX = (index < 2) ? index * btnW : (index + 1) * btnW
                view.frame = CGRect(x: buttonX, y: btnY, width: btnW, height: BarHeight)
                index += 1
            }
        }
    }
}

